import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref(null)
    const isSessionValidated = ref(false)

    // 设置用户信息
    const setUserInfo = (info) => {
      userInfo.value = info
    }

    // 清除用户信息
    const clearUserInfo = () => {
      userInfo.value = null
    }

    // 获取用户ID
    const getUserId = () => {
      return userInfo.value?.id
    }

    const setSessionValidated = (validated) => {
      isSessionValidated.value = !!validated
    }

    return {
      userInfo,
      isSessionValidated,
      setUserInfo,
      clearUserInfo,
      getUserId,
      setSessionValidated,
    }
  },
  {
    persist: {
      // 持久化配置
      key: 'user-store', // 存储的key
      storage: localStorage, // 存储方式
      paths: ['userInfo'], // 需要持久化的数据路径（不要持久化 isSessionValidated）
    },
  },
)
